package com.shinewind.task.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shinewind.pojo.entity.Rate;
import com.shinewind.pojo.entity.Rating;
import com.shinewind.task.mapper.IRateMapper;
import com.shinewind.task.service.IModelMappingService;
import com.shinewind.task.service.IRateService;
import com.shinewind.task.service.IUserInfoMappingService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RateServiceImpl extends ServiceImpl<IRateMapper, Rate> implements IRateService {
    @Autowired
    private IModelMappingService iModelMappingService;
    @Autowired
    private IUserInfoMappingService iUserInfoMappingService;

    @Override
    public Long getCountAndRateBymId(String mId) {
        Long count = this.lambdaQuery().eq(Rate::getMId,mId).count();
        return count;
    }

    @Override
    public Double getSumAndRateBymId(String mId) {
        List<Rate> rates = this.lambdaQuery().eq(Rate::getMId,mId).select(Rate::getRate).list();
        Double sum = 0.0;
        for(Rate rate : rates){
            sum = sum + rate.getRate();
        }
        return sum;
    }

    @Override
    public Long getCountAndRateByuId(String uId) {
        Long count = this.lambdaQuery().eq(Rate::getUId,uId).count();
        return count;
    }

    @Override
    public Double getRateByuId(String uId,String mId) {
        Rate rate = this.lambdaQuery().eq(Rate::getUId,uId).eq(Rate::getMId,mId).one();
        return rate.getRate();
    }

    @Override
    public List<Rating> getAllRates() {
        List<Rate> rates = this.lambdaQuery().list();
        List<Rating> ratings = rates.stream().map(rate ->{
            Rating rating = Rating.builder().build();
            Integer userId = iUserInfoMappingService.getMappingUidByStrUid(rate.getUId());
            Integer modelId = iModelMappingService.getMappingMidByStrMid(rate.getMId());
            rating.setUserId(userId);
            rating.setModelId(modelId);
            rating.setRating(rate.getRate());
            return rating;
        }).collect(Collectors.toList());
        return ratings;
    }
}
